package ca.abubakrsagar.breadboardsim;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PropertiesPanel extends JPanel {
	private int position; // The position of the component in the array list
	private int type; // the type of component
	private BreadboardPanel breadboardPanel; // The instance of the breadboardPanel for
										// this panel
	private BreadBoardPanelController breadboardPanelController; // The controller for
															// that will listen
															// to the components

	private JLabel LED = new JLabel("LED"); // The "LED" label
	private JLabel resistor = new JLabel("Resistor"); // The "Resistor" Label
	private JLabel Switch = new JLabel("Switch"); // The "Switch" label
	private JLabel wire = new JLabel("Wire"); // The "Wire" Label
	private JLabel ohms = new JLabel("Ohms:"); // The "Ohms" Label
	private JLabel ohmsValue = new JLabel(); // The label that will display the value of
										// the ohms
	private JLabel changeOhms = new JLabel("Change:"); // The "Change" label
	private JLabel switchState = new JLabel("State:"); // The "State" Label
	private JLabel switchStateValue = new JLabel("test"); // The label that will display
													// the state of the switch

	private JTextField resistance = new JTextField(); // The textfield that will allow
												// you to change the resistance

	private TextFieldController resistanceController; // The textfield controller
	private DropDownMenuController switchController; // The dropdown menu controller.

	private final String[] OPTIONS = { "Off", "On" }; // Options on the drop down menu.
	private JComboBox dropDownMenu = new JComboBox(OPTIONS); // The dropdown menu.

	/**
	 * This is the constructor for the Properties Panel. It creates a new
	 * properties panel and adds all the components to it as well as sets their
	 * properties.
	 * 
	 * @param BreadboardPanel
	 *            The instance of the BreadboardPanel that will be linked to
	 *            this panel
	 */
	public PropertiesPanel(BreadboardPanel breadboardPanel) {
		this.setLayout(null);
		this.breadboardPanel = breadboardPanel;

		breadboardPanelController = new BreadBoardPanelController(
				breadboardPanel, this);
		resistanceController = new TextFieldController(this, breadboardPanel);
		switchController = new DropDownMenuController(breadboardPanel, this);

		this.setPreferredSize(new Dimension(900 / 5, 506 / 2));
		this.setBorder(BorderFactory.createTitledBorder("Properties"));

		this.add(LED);
		this.add(resistor);
		this.add(Switch);
		this.add(wire);
		this.add(resistance);
		this.add(ohms);
		this.add(ohmsValue);
		this.add(changeOhms);
		this.add(switchState);
		this.add(switchStateValue);
		this.add(dropDownMenu);

		LED.setBounds(20, 20, 25, 10);
		LED.setVisible(false);

		resistor.setBounds(20, 20, 50, 10);
		ohms.setBounds(20, 40, 50, 10);
		ohmsValue.setBounds(80, 40, 200, 10);
		changeOhms.setBounds(20, 60, 50, 20);
		resistance.setBounds(80, 60, 50, 20);
		ohms.setVisible(false);
		ohmsValue.setVisible(false);
		resistance.setVisible(false);
		changeOhms.setVisible(false);
		resistor.setVisible(false);

		Switch.setBounds(20, 20, 50, 10);
		switchState.setBounds(20, 40, 50, 10);
		switchStateValue.setBounds(80, 40, 50, 10);
		dropDownMenu.setBounds(20, 60, 50, 20);
		Switch.setVisible(false);
		switchState.setVisible(false);
		switchStateValue.setVisible(false);
		dropDownMenu.setVisible(false);

		wire.setBounds(20, 20, 50, 10);
		switchState.setBounds(20, 40, 50, 10);
		wire.setVisible(false);

		resistance.addActionListener(resistanceController);
		dropDownMenu.addActionListener(switchController);

	}

	/**
	 * Updates the properties panel to display information based on the
	 * component clicked.
	 */
	public void display() {
		setVisibleFalse();
		if (type == 1) {
			LED.setVisible(true);

		} else if (type == 2) {
			System.out.println(position);
			int ohmsV = breadboardPanel.getBreadboard().getResistor()
					.get(position).getOhms();
			ohmsValue.setText(String.valueOf(ohmsV));
			ohms.setVisible(true);
			ohmsValue.setVisible(true);
			resistance.setVisible(true);
			changeOhms.setVisible(true);
			resistor.setVisible(true);
		} else if (type == 3) {
			boolean state = breadboardPanel.getBreadboard().getSwitch()
					.get(position).isOn();
			if (state) {
				switchStateValue.setText("On");
			} else {
				switchStateValue.setText("Off");
			}
			Switch.setVisible(true);
			switchState.setVisible(true);
			switchStateValue.setVisible(true);
			dropDownMenu.setVisible(true);
			dropDownMenu.setVisible(true);

		}

	}

	/**
	 * @param position The position of the properties panel.
	 */
	public void setPosition(int position) {
		this.position = position;
	}

	/**
	 * @param type Type type of the properties panel.
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * Hide all information on the properties panel.
	 */
	public void setVisibleFalse() {
		ohms.setVisible(false);
		ohmsValue.setVisible(false);
		resistance.setVisible(false);
		changeOhms.setVisible(false);
		resistor.setVisible(false);
		LED.setVisible(false);
		wire.setVisible(false);
		switchState.setVisible(false);
		Switch.setVisible(false);
		switchStateValue.setVisible(false);
		dropDownMenu.setVisible(false);
	}

	/**
	 * @return The position on the properties panel.
	 */
	public int getPosition() {
		return position;
	}

	/**
	 * @return The type of properties panel.
	 */
	public int getType() {
		return type;
	}

	/**
	 * @return The breadboard panel controller.
	 */
	public BreadBoardPanelController getBreadboardPanelController() {
		return breadboardPanelController;
	}

	/**
	 * @return The text field.
	 */
	public JTextField getTextField() {
		return resistance;
	}

	/**
	 * @return The dropdown menu.
	 */
	public JComboBox getDropDown() {
		return dropDownMenu;
	}

}
